﻿using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Lrd.Template.Core.Infrastructure.Dependency;
using Lrd.Template.Core.Infrastructure.Enums;
using Lrd.Template.Core.Infrastructure.Extensions;

namespace Lrd.Template.Core.Infrastructure.EntityHistory
{
    /// <summary>
    /// 审计实体通知处理类
    /// </summary>
   public  class AuditEntityEntryHandler : INotificationHandler<AuditEntityEntryNotification>
    {

        private readonly ScopedDictionary _scopedDictionary;

        public AuditEntityEntryHandler(ScopedDictionary scopedDictionary)
        {
            _scopedDictionary = scopedDictionary;
        }

        public  Task Handle(AuditEntityEntryNotification notification, CancellationToken cancellationToken)
        {
            notification.NotNull(nameof(notification));
            cancellationToken.ThrowIfCancellationRequested();
            var auditLog=  _scopedDictionary.AuditLog;
            if (auditLog == null)
            {
                return Task.CompletedTask;
            }
            foreach (var auditEntity in notification.AuditEntities)
            {
              
                auditLog.AuditEntityEntries.Add(auditEntity);
            }
            return Task.CompletedTask;
        }
    }
}
